﻿$(function(){
	var loginClickBtn, retrievePwd = "#retrievePwd", cookieUser = "cookieUser" ;
	
	$("#fullpage").fullpage({
		sectionsColor: ['#8FB98B', '#DE564B', '#F9F3DC'],
		anchors: ['login', 'iForgot', 'help'],
		slidesNavigation: true,
		navigation: false,
		menu: '#menu',
		afterRender: function(){
		},
		afterLoad: function(anchorLink, index){
		},
		onLeave: function(index, direction){
		}
	});
	
	
	initCookie() ;
	
	var l = $(".login-btn").bind("click", loginClickBtn = function(){
		loginValid() ;
	});
	
	function loginValid(){
		l.unbind("click");
		var account = $("input[name=account]").val();
		var password = $("input[name=password]").val();
		
		$.post($.ctx+"/login/loginValid", {"account": account, "password": password}, function(result) {
			console.info(result);
			if(result.status) {
				setTimeout(function(){		
					window.location.replace($.ctx+"/admin");
				},500);
			} else {
				l.bind("click", loginClickBtn);
			}
		},"JSON");		
	}
	
	/**
	 * 找回密码
	 */
	$(retrievePwd).click(function(){
		var account = $("input[name=iforgot-accouont]").val() ;
		if(undefined == account || "" == account.trim()) {alert("请输入账号！"); return;}
		
		$.post($.ctx+"/admin/sys/user/iForgotPwd", {"account": account}, function(result) {
			if(result.status) {
				$(".mail-info").html(result.message);
			} else {
				$(".mail-info").html(result.message);
			}
		},"JSON");		
	});

	function initCookie() {
		var loginUser = $.cookie(cookieUser);
		if (undefined != loginUser && "" != loginUser) {
			var splits = loginUser.split("[#]");
			$("input[type=text][name=login-username]").val($.base64.decode(splits[0]))
			$("input[type=password][name=login-password]").val($.base64.decode(splits[1]))
			$("#Remember").prop("checked", true);
		} else {
			$("input[type=text][name=login-username]").val("");
			$("input[type=password][name=login-password]").val("");
			$("#Remember").prop("checked", false);
		}
	}
	function setCookie(name, value) {
		$.cookie(name, value, {
			isStrict: true,
			expires : 30,
			path : "/"
		});
	}
	function removeCookie(name) {
		$.cookie(name, null, {
			expires : -1,
			path : "/"
		});
	}
});


